package com.mxl.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列
 * @author mxl
 * @date 2021-10-08
 */
//@Configuration
public class DelayedConfig {

    public static final String QUEUE_NAME_DELAYED = "mxl.delayed";
    public static final String EXCHANGE_NAME_DELAYED = "mxl.delayed";
    public static final String ROUTING_KEY_DELAYED = "DELAY.#";

    @Bean
    Queue delayedQueue() {
        return new Queue(QUEUE_NAME_DELAYED, true);
    }

    @Bean
    CustomExchange delayedExchange() {
        Map<String, Object> arguments = new HashMap<>(1);
        // 在这里声明一个主题类型的延迟队列，当然其他类型的也可以。
        arguments.put("x-delayed-type", "topic");
        return new CustomExchange(EXCHANGE_NAME_DELAYED, "x-delayed-message", true, false, arguments);
    }

    @Bean
    Binding bindingDelayed() {
        return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(ROUTING_KEY_DELAYED).noargs();
    }
}
